package com.lsmo.android.carfans.view.forum;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.lsmo.android.carfans.R;
import com.lsmo.android.carfans.model.forum.ForumThread;
import com.lsmo.android.carfans.process.forum.ForumProcess;
import com.lsmo.android.common.base.BaseProcess;
import com.lsmo.android.common.base.BaseTask;
import com.lsmo.android.common.base.ViewProcessCallBack;

/****
 * 
*
* @类描述：  热帖                                                   
* @项目名称：carfans     
* @类名称：ForumHotView                    
* @修改时间：2012-3-24 上午08:49:22    
* @修改备注：                                                              
*
 */
public class ForumHotView extends  LinearLayout implements ViewProcessCallBack   {

	public static final String TAG = ForumHotView.class.getSimpleName();
	
	private ForumProcess mProcess; 
    private ListView mListView;
    private Context mContext ;
    private ViewGroup mProgressBarLayout ;
    
	public ForumHotView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		
		
		this.mProcess = new ForumProcess();
		this.mContext = context ;
		 
		((Activity)context).getLayoutInflater().inflate(R.layout.forum_hot_layout, this);
 
		this.postInvalidate();
		mListView = (ListView)this.findViewById(R.id.forum_hot_listview);
		mProgressBarLayout = (ViewGroup)this.findViewById(R.id.loading_layout);
		updateView();
 
 
	}
	
	
	@Override
	public void processAfterTaskFinish(int code, Object result ) {
		// TODO Auto-generated method stub
	
		if(code == ForumHotTask.GET_HOT_THREADS_LIST){
			if(result!=null){
 
				mListView.setAdapter(new ForumHotListAdapter(mContext, (List<ForumThread>)result)) ;
				mListView.postInvalidate();
 
			}
		}
		
		this.mProgressBarLayout.setVisibility(View.GONE);
		this.mListView.setVisibility(View.VISIBLE);
 
	}
	
	
	@Override
	public void updateView() {
		// TODO Auto-generated method stub
		 
			// if(!isFirstRun){
			//		isFirstRun = true ;
	
		this.mProgressBarLayout.setVisibility(View.VISIBLE);
		this.mListView.setVisibility(View.GONE);
		
					new ForumHotTask(ForumHotTask.GET_HOT_THREADS_LIST,
							this.mContext,
							this,
							this.mProcess 	
				 ).execute(new String[]{});
		//	  }
		
	}
	
	
	private  class ForumHotListAdapter extends BaseAdapter{

		private  List<ForumThread> list ;
 		private Context mContext ;
		public ForumHotListAdapter(Context context, List<ForumThread> l) {
			// TODO Auto-generated constructor stub
			
			this.list = l ;
			this.mContext = context ;
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			
			if(convertView==null){
				convertView = new ForunThreadListItemLayout(this.mContext); 
			}
			
			
			((ForunThreadListItemLayout)convertView).updateView(this.list.get(position));
			
			
			return convertView;
		}
		
		
		
	}
	
	
	private class ForumHotTask extends BaseTask{

		 

			public static final int GET_HOT_THREADS_LIST = 1 ; //查询热门帖子列表
			private ForumProcess mProcess ;
 	
			public ForumHotTask(int code , Context context,ViewProcessCallBack view,BaseProcess process ) {
				// TODO Auto-generated constructor stub
			    super(code,context,view  );
				this.mProcess = (ForumProcess)process ;	
			}
			
 
			@Override
			protected void onPreExecute() {
				// TODO Auto-generated method stub
				super.onPreExecute();
	 
			}
			
		
			
			@Override
			protected Object doInBackground(String... params) {
				// TODO Auto-generated method stub
				super.doInBackground(params);
				Object result = null;
		 
				switch(this.mCode){
					case GET_HOT_THREADS_LIST:
					
						result = mProcess.getHotThreadsList(params);
						
					break;
				}
				return result;
			}
			
			
			@Override
			protected void onPostExecute(Object result) {
				// TODO Auto-generated method stub
				super.onPostExecute(result);
				
				switch(this.mCode){
				case GET_HOT_THREADS_LIST:
				
				 	 mView.processAfterTaskFinish(this.mCode ,result);
					
				break;
			   }
 	
			}
		 
		}


}
	
	 
 
